textureId   = -1;
modelPlayer = -1;
modelBrick  = -1;
modelGoal   = -1;
playerCollisionId = -1;
offset = 0;
brickTable = {};
g = -9.8 * (100) / 60 / 60 ;
a = g

-- startup
function Init()
	Stage_Load(Stage_Instance(), "stage/stage1.bmp")

	textureId   = GR_CreateTextureFromFileS(GR_Instance(), "textures/texture.png")

	modelPlayer = GR_RegisterModelS(GR_Instance(), textureId, 32.0, 32.0, 0.0,      0.0, 1.0/16.0, 1.0/16.0)
	modelBrick  = GR_RegisterModelS(GR_Instance(), textureId, 32.0, 32.0, 2.0/16.0, 0.0, 1.0/16.0, 1.0/16.0)
	modelGoal   = GR_RegisterModelS(GR_Instance(), textureId, 32.0, 32.0, 3.0/16.0, 0.0, 1.0/16.0, 1.0/16.0)

	offset = 0

	local stageElementToCollisionType = {
		[SE_Brick] = CT_Brick,
		[SE_Player] = CT_Player,
	};
	
	for x = 0,32 do
		for y = 0,15 do
			local se = Stage_GetStageElement(Stage_Instance(), x, y);
			local collisionType = stageElementToCollisionType[se]
			if collisionType ~= nil then
				local collisionId = Collision_RegisterS(Collision_Instance(), x*32.0, y*32.0, 32.0, 32.0, collisionType)
				if collisionType == CT_Player then 
					playerCollisionId = collisionId
				end
				if collisionType == CT_Brick then
					table.insert(brickTable, collisionId)
				end
			end
		end
	end
end

-- end
function Term()
	GR_UnregisterModel(GR_Instance(), modelGoal)
	modelGoal = -1;

	GR_UnregisterModel(GR_Instance(), modelBrick)
	modelBrick = -1;
	
	GR_UnregisterModel(GR_Instance(), modelPlayer)
	modelPlayer = -1;
	
	GR_ReleaseTexture(GR_Instance(), textureId)
	textureId = -1;
end

-- update
function Update()
	local vx = 0.0
	local vy = 0.0
	
	if false ~= IN_IsPressed(IN_Instance(), KS_Right) then
		vx = 4.0
	end
	if false ~= IN_IsPressed(IN_Instance(), KS_Left) then
		vx = -4.0
	end
	if false ~= Collision_ResultIsLanded(Collision_Instance(), playerCollisionId) then
		if false ~= IN_IsPressedTrigger(IN_Instance(), KS_Up) then
			a = 9
		end
	else
		if false ~= IN_IsReleasedTrigger(IN_Instance(), KS_Up) then
			if 0 < a then
				a = 0
			end
		end
	end
	
	vy = vy + a
	a = a + g
	if a < -6 then
		a = -6
	end
	
	Collision_MoveS(Collision_Instance(), playerCollisionId, vx, vy)

	local xL = Collision_ResultGetPosLeftX(Collision_Instance(), playerCollisionId)
	local yB = Collision_ResultGetPosBottomY(Collision_Instance(), playerCollisionId)
	GR_PutSpriteS(GR_Instance(), modelPlayer, xL-offset, yB, 0.5, 0xffffffff)

	if (CR_NotCollided ~= Collision_ResultGetResultType(Collision_Instance(), playerCollisionId)) then
		a = g
	end
	
	for i,v in ipairs(brickTable) do
		local collisionResult = Collision_ResultGetResultType(Collision_Instance(), v)
		if CR_Lose == collisionResult then
			table.remove(brickTable, i)
			Collision_Unregister(Collision_Instance(), v)
		else
			local xL = Collision_ResultGetPosLeftX(Collision_Instance(), v)
			local yB = Collision_ResultGetPosBottomY(Collision_Instance(), v)
			GR_PutSpriteS(GR_Instance(), modelBrick, xL-offset, yB, 0.5, 0xffffffff)
		end
	end
	
end

